package com.ddy.test.creditcardmanage

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import com.ddy.test.creditcardmanage.model.Card
import com.ddy.test.creditcardmanage.model.CardViewModel
import kotlinx.android.synthetic.main.add_card.*

class ModifyCard: AppCompatActivity() {
    companion object {
        private const val LOG_TAG: String = "CCM_UPDATE"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.add_card)

        val cardId = this.intent.getLongExtra("cardId", 0)
        title = "UpdateCard"
        val  card = CardViewModel(this.application).getCard(cardId)
        cardComment.setText(card.comment)
        cardNo.setText(card.cardNo)
        cardPermanentLine.setText(("" + card.permanentLine))
        cardTemporaryLine.setText(("" + card.temporaryLine))
        cardStatementAmount.setText(("" + card.statementAmount))
        cardStatementDate.minValue=1
        cardStatementDate.maxValue=28
        cardDueDate.minValue = 1
        cardDueDate.maxValue = 28
        cardStatementDate.value = card.statementDate
        cardDueDate.value = card.dueDate


    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        val inflater = menuInflater
        inflater.inflate(R.menu.update, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val cardId = this.intent.getLongExtra("cardId", 0)
        return when (item.itemId) {
            R.id.action_update_save -> {
                val varCardNo: String = cardNo.text.toString()
                if (varCardNo.trim() == ""){
                    Toast.makeText(this, "please input the card No.", Toast.LENGTH_LONG).show()
                    return false
                }
                val varCardComment = cardComment.text.toString().trim()
                val varCardPermanentLine = cardPermanentLine.text.toString().trim()
                val varCardTemporaryLine = cardTemporaryLine.text.toString().trim()
                val varCardStatementAmount = cardStatementAmount.text.toString().trim()
                val varCardStatementDate = cardStatementDate.value
                val varCardDueDate = cardDueDate.value

                val cardViewModel = CardViewModel(this.application)
                val  card = cardViewModel.getCard(cardId)
                if( varCardNo != card.cardNo ||
                    varCardComment != card.comment ||
                    varCardPermanentLine.toFloat() != card.permanentLine ||
                    varCardTemporaryLine.toFloat() != card.temporaryLine ||
                    varCardStatementAmount.toFloat() != card.statementAmount ||
                    varCardStatementDate != card.statementDate ||
                    varCardDueDate != card.dueDate){

                    val cardForm: Card = Card.newCard(varCardNo, varCardComment, varCardPermanentLine.toFloat(),
                        varCardTemporaryLine.toFloat(), varCardStatementAmount.toFloat(), varCardStatementDate,varCardDueDate)
                    cardForm.id = card.id

                    cardViewModel.updateCard(cardForm)
                    Log.d(LOG_TAG,"update card for " + card.id)
                }else{
                    Log.w(LOG_TAG, "no update for " + card.id)
                }

                startActivity(Intent(this, MainActivity().javaClass).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
}